<script setup lang="ts">
/**
 * 系统管理-系统数据-字典管理-编辑字典
 */
import {ElMessage, type FormInstance} from 'element-plus'
import {editLabelApi} from '@/assets/api/systemManagement/data/label'
import {ref} from 'vue'
import {labelTypeList} from '@/components/baseTs/labelType'
import type {DataLabel} from "@/components/interface/data/dataLabel";


const props = defineProps<{
  form: DataLabel,
  disabled: boolean
}>()

const emit = defineEmits(['save', 'close'])

// 表单验证
const formRef = ref()
const rules = {
  name: [
    {required: true, message: '请输入名称', trigger: 'blur'}
  ],
  tag: [
    {required: true, message: '请输入标签', trigger: 'blur'}
  ],
  type: [
    {required: true, message: '请输入类型', trigger: 'blur'}
  ]
}

// 保存
const saveLoading = ref(false)
const save = async (formEl: FormInstance) => {
  if (!formEl) return
  await formEl.validate((valid: boolean) => {
    if (valid) {
      saveLoading.value = true
      editLabelApi(props.form).then(res => {
        saveLoading.value = false
        if (res.data.code === 200) {
          ElMessage.success({message: res.data.data, showClose: true})
          emit('save', true)
        } else {
          ElMessage.error({message: res.data.data, showClose: true})
        }
      })
    }
  })
}
</script>

<template>
  <div class="form-div">
    <el-form v-loading="saveLoading" :model="form" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="form.tag" placeholder="请输入标签"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" style="width: 200px" placeholder="请选择标签分类">
          <el-option v-for="item in labelTypeList" :key="item.value" :label="item.label"
                     :value="item.value" :disabled="disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="父级ID" prop="pid">
        <el-input v-model="form.pid" placeholder="请输入父级ID" :value="0"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
